﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WallpaperForms.Dtos;

namespace WallpaperForms.Commons
{
    public  class WallPaperFile
    {
        /// <summary>
        /// 读取文件对象
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ProjectJsonDto GetFileObj(string path)
        {
            var fileStream = new FileStream(path + "\\project.json", FileMode.Open);
            var bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            var obj = JsonConvert.DeserializeObject<ProjectJsonDto>(Encoding.UTF8.GetString(bytes));
            obj.dirPath = path;
            obj.objKey = Guid.NewGuid().ToString("N");
            if (File.Exists(Path.Combine(obj.dirPath, obj.file)))
            {
                var fileSizeBytes = new FileInfo(Path.Combine(obj.dirPath, obj.file)).Length;
                obj.fileSizeMB = (double)fileSizeBytes / 1024 / 1024;
            }
            fileStream.Close();
            fileStream.Dispose();
            return obj;
        }
    }
}
